package com.xiyue.server.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sport.common.utils.PasswordUtils;
import com.sport.model.dto.Option;
import com.sport.model.entity.SysPermission;
import com.sport.model.entity.SysUser;
import com.xiyue.server.mapper.SysUserMapper;
import com.xiyue.server.service.SysUserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Resource
    SysUserMapper sysUserMapper;
    @Override
    public int addSysUser(SysUser sysUser) {
        String salt = RandomUtil.randomString(16);
        sysUser.setSalt(salt);
        String password= sysUser.getPassword();
        sysUser.setPassword(PasswordUtils.encrypt(sysUser.getSalt(),password));
        return sysUserMapper.insert(sysUser);
    }

    @Override
    public List<Option> getRoles(String id) {
        /**
         *  通过用户id找角色
         */
        return baseMapper.getRolesByUserId(id);
    }

    @Override
    public void insertRoles(String id, List<Object> roleIds) {
        baseMapper.insertRoleIds(id,roleIds);
    }

    @Override
    public void deleteRole(String id) {
        baseMapper.deleteByUserId(id);
    }

    @Override
    public List<String> listRoleByUserId(String userId) {
        return baseMapper.getRoleCodeByUserId(userId);
    }

    @Override
    public List<String> listPermissionByRoleCode(String roleCode) {
        return baseMapper.getPermissionByRoleCode(roleCode);
    }

    @Override
    public List<JSONObject> listMenuByUserId() {
        /**
         *  根据当前用户取得角色
         *  接着查询菜单
         */
        List<JSONObject> data = new ArrayList<>();
        //获取到父级菜单
        List<SysPermission> parentMenus = baseMapper.listParentMenuByUserId(StpUtil.getLoginIdAsString());
        for(SysPermission permission : parentMenus){
            // 构造前端菜单数据
            JSONObject parentMenu = new JSONObject();
            JSONObject parentMata = new JSONObject();

            parentMenu.putOnce("path", permission.getPath());
            parentMenu.putOnce("component", "LAYOUT");

            getMenuTitle(permission, parentMenu, parentMata);
            // 获取子级菜单
            List<SysPermission> childrenList = baseMapper.listMenuByUserId(StpUtil.getLoginIdAsString(),permission.getId());
            List<JSONObject> children = new ArrayList<>();
            for(SysPermission child : childrenList){
                JSONObject childMenu = new JSONObject();
                JSONObject childMata = new JSONObject();

                childMenu.putOnce("path", child.getPath());
                childMenu.putOnce("name", child.getPath());

                if(child.getOuterChain() == 1){
                    childMenu.putOnce("component", "IFrame");
                    childMata.putOnce("frameSrc",child.getCode());
                }else childMenu.putOnce("component", child.getCode());
                getMenuTitle(child,childMenu,childMata);
                children.add(childMenu);
            }
            parentMenu.putOnce("children", children);
            data.add(parentMenu);
        }
        log.info("——————————————{}",data);
        return data;
    }

    private void getMenuTitle(SysPermission menuInfo,JSONObject menu,JSONObject mata){
        JSONObject menuTitle = new JSONObject();
        menuTitle.putOnce("zh_CN",menuInfo.getName());
        menuTitle.putOnce("en_US",menuInfo.getEnglishName());
        mata.putOnce("title", menuTitle);
        mata.putOnce("icon", menuInfo.getIcon());
        menu.putOnce("meta", mata);
    }


}
